package com.frejoys.common.jsonserializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;

import java.io.IOException;

/**
 * Js中可处理的数据范围是-9007199254740992 ~ 9007199254740992
 * Java中数字长度或是超过这个限制
 * 将Long类型数字转换为字符串
 */
public class BigNumberSerializer extends NumberSerializer {

    // private static final long JS_NUM_MAX = 9007199254740992L;
    // private static final long JS_NUM_MIN = -9007199254740992L;
    public static final BigNumberSerializer instance = new BigNumberSerializer(Number.class);

    public BigNumberSerializer(Class<? extends Number> rawType) {
        super(rawType);
    }

    public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException {

        gen.writeString(value.toString());

        // long longValue = value.longValue();
        // if (longValue >= JS_NUM_MIN && longValue <= JS_NUM_MAX) {
        //     super.serialize(value, gen, provider);
        // } else {
        //     gen.writeString(value.toString());
        // }

    }

}
